classdef ziServer < handle
    %ZIDATASERVER �˴���ʾ�йش����ժҪ
    %   �˴���ʾ��ϸ˵��
    
    properties
        deviceSerials
        deviceProps
    end
    
    properties(Access=private)
        demodSignalNodes
        maxDataTransferRate
    end
    
    methods
        function obj = ziServer(device_ids)
            %ZIDATASERVER ��������ʵ��
            %   �˴���ʾ��ϸ˵��
            p=inputParser;
            p.addRequired('device_ids', @iscell);
            p.parse(device_ids);
            
            obj.deviceSerials = device_ids;
            obj.deviceProps = cellfun(@(devSer) ziDAQ('discoveryGet', devSer), obj.deviceSerials);
            obj.demodSignalNodes = {};
            obj.maxDataTransferRate = 0.0;
        end
        
        function clockbase = getClockbase(~, device)            
            clockbase = ziDAQ('getDouble', ['/' device '/clockbase']);
        end
        function mdsStatus = getMdsStatus(~, group)
            if nargin == 1
                group = 0;
            end
            mdsStatus = ziDAQ('getInt', sprintf('/ZI/MDS/GROUPS/%d/status', group));
        end
        
        function disp(obj)
            if ~isempty(obj.demodSignalNodes)
                titleStr = sprintf('%d Node(s) have been subscribed to the ziServer.', size(obj.demodSignalNodes, 1));
                lineStr = repmat('-', 1, length(titleStr));
                fprintf('%s\n%s\n', titleStr, lineStr);
                for k = 1:size(obj.demodSignalNodes, 1)
                    nodeStr = ['/' obj.demodSignalNodes{k, 1} '/' obj.demodSignalNodes{k,2}.toDispString];
                    spaceStr = repmat(' ', 1, max(1, 35-length(nodeStr)));
                    fprintf('\tNode(%d) := %s %s [%d, %d, %d]\n', k, nodeStr, spaceStr, obj.demodSignalNodes{k, 3});
                end
                fprintf('The max sampling rate of these nodes is %3.2e\n', obj.maxDataTransferRate); 
            else
                fprintf('No node was subscribed to the ziServer.\n.');
            end
        end
                
        %%
        function mds = multiDeviceSyncModule(obj, name)
            if nargin == 1
                name = 'mds';
            end
            mds = EquipmentControl.ZI.ziServer.ServerModules.MultiDeviceSyncModule(obj, name);
        end
        
        function plotter = plotterModule(obj, name)
            plotter = EquipmentControl.ZI.ziServer.ServerModules.PlotterModule(obj, name);
        end
        
        function spect = spectrumModule(obj, name)
            spect = EquipmentControl.ZI.ziServer.ServerModules.SpectrumModule(obj, name);
        end
        
        function daq = dataAcquisitionModule(obj, name)
            if nargin == 1
                name = 'daq';
            end
            daq = EquipmentControl.ZI.ziServer.ServerModules.DataAcquisitionModule(obj, name);
        end
        
        function scp = scopeModule(obj, name)
            if nargin == 1
                name = 'scp';
            end
            scp = EquipmentControl.ZI.ziServer.ServerModules.ScopeModule(obj, name);
        end
        
        function swp = sweeperModule(obj, name)
            if nargin == 1
                name = 'swp';
            end
            swp = EquipmentControl.ZI.ziServer.ServerModules.SweeperModule(obj, name);
        end
        
        %%
        function obj = subscribe_DemodSignal(obj, device, node, varargin)
            L = log4m.getLogger;
            p=inputParser;
            p.addRequired('device', @(x) ismember(x, obj.deviceSerials));
            p.addRequired('node', @(x) isa(x, 'ziStreamNode'));
            p.addOptional('group', [1, 1, 1], @(x) isnumeric(x) && length(x) == 3 && x(3)<=x(1)*x(2));
            p.parse(device, node, varargin{:});            
            
            rate = obj.getDataTransferRate(device, node);
            if rate > obj.maxDataTransferRate
                obj.maxDataTransferRate = rate;
                L.info('ziServer::subscribe_DemodSignal', sprintf('Max. Sampling Rate is updated to [%3.2e]', rate));
            end
            
            obj.demodSignalNodes = [obj.demodSignalNodes; {device, node, p.Results.group}];
        end
        function obj = unsubscribe_DemodSignal(obj)
            obj.demodSignalNodes = {};
            obj.maxDataTransferRate = 0.0;
        end
        
        function rate = enableDataTransfer(obj, device, node)
            getSession;
            zi = sess.getDevice(device);
            
            if strcmp(node.node, 'demods')
                zi.enableDataTransfer(node.index);
            end
            rate = obj.getDataTransferRate(device, node);
            
        end
        
        function rate = getDataTransferRate(~, device, node)
            getSession;
            zi = sess.getDevice(device);
            
            switch node.node
                case 'demods'
                    rate = zi.getDataTransferRate(node.index);
                case 'pids'
                    rate = zi.getPIDStreamValue(node.index);
            end
        end
        
        function [new_rate, old_rate] = setDataTransferRate(~, device, node, rate)
            getSession;
            zi = sess.getDevice(device);
            
            switch node.node
                case 'demods'
                    old_rate = zi.getDataTransferRate(node.index);
                    zi.setDataTransferRate(node.index, rate);
                    new_rate = zi.getDataTransferRate(node.index);
                case 'pids'
                    old_rate = zi.getPIDStreamValue(node.index);
                    zi.setPIDStreamValue(node.index, rate);
                    new_rate = zi.getPIDStreamValue(node.index);
            end
        end
    end
    
    methods
        res = viewScopeSignal(obj, ch1, ch2, trig, varargin)
        res = viewDemodSignal(obj, varargin)
        res = viewDemodSpectrum(obj, nCol, varargin)
        res = viewParamDependance(obj, device, node, start, stop, varargin)
    end
end

